const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MdToHtmlPlugin = require('./plugins/md-to-html-plugin');
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const WebpackBuildNotifierPlugin = require('webpack-build-notifier');
// const DashboardPlugin = require('webpack-dashboard/plugin');
// const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const BundleAnalyzerPlugin =
  require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const smp = new SpeedMeasurePlugin();

const config = {
  mode: 'development',
  entry: resolve(__dirname, 'src/index.js'),
  output: {
    path: resolve(__dirname, 'build'),
    filename: 'bundle.[hash].js',
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.json'],
  },
  resolveLoader: {
    modules: ['node_modules', resolve(__dirname, 'loaders')],
  },
  module: {
    rules: [
      {
        test: /\.tpl$/,
        use: [
          'babel-loader',
          {
            loader: 'tpl-loader',
            options: {
              log: true,
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: resolve(__dirname, 'index.html'),
    }),
    new MdToHtmlPlugin({
      template: resolve(__dirname, 'public/test.md'),
      filename: 'test.html',
    }),
    new WebpackBuildNotifierPlugin({
      title: '我的webpack',
      suppressSuccess: true,
    }),
    new BundleAnalyzerPlugin({
      analyzerMode: 'server',
      analyzerHost: '127.0.0.1',
      analyzerPort: 8889,
      reportFilename: 'report.html',
      defaultSizes: 'parsed',
      openAnalyzer: true,
      generateStatsFile: false,
      statsFilename: 'stats.json',
      statsOptions: null,
      logLevel: 'info',
    }),
    new CleanWebpackPlugin()
  ],
  devServer: {
    port: 3333,
  },
  optimization: {
    minimizer: [
      new TerserPlugin({
        parallel: true,
        terserOptions: {
          ecma: 6,
        },
      }),
    ],
  },
};

module.exports = smp.wrap(config);
